package com.hss.backservice.service;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;

import com.hss.backservice.IMyAidlInterface;


/**
 * <b>Project:</b> NDKNoKillService<br>
 * <b>Create Date:</b> 2016/12/20 0020<br>
 * <b>Author:</b> huang saisai <br>
 * <b>Description:</b> <br>
 */
public class RemoteService extends Service {

    MyBinder myBinder;
    @Override
    public void onCreate() {
        super.onCreate();
        if (null == myBinder){
            myBinder = new MyBinder();
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }

    class  MyBinder extends IMyAidlInterface.Stub{

        @Override
        public String getName(String name) throws RemoteException {
            return "RemoteService" + name;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Intent service = new Intent(this,LocalService.class);
        bindService(service, connection,Service.BIND_IMPORTANT);
        return Service.START_STICKY;
    }

    ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            IMyAidlInterface inttr    = IMyAidlInterface.Stub.asInterface(iBinder);

            Log.e("RemoteService","onServiceConnected");
            try {
                Log.e("",inttr.getName("yy"));
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Intent serviceIntent = new Intent(RemoteService.this, LocalService.class);
            startService(serviceIntent);
            bindService(serviceIntent, connection,Service.BIND_IMPORTANT);
        }
    };
}
